@echo off
set version=%1

if not exist "sqlite-amalgamation-%version%.zip" (
    echo fetching sqlite-amalgamation-%version%.zip ... 
    wget http://www.sqlite.org/sqlite-amalgamation-%version%.zip
    wget http://www.sqlite.org/2013/sqlite-amalgamation-%version%.zip
    if not exist "sqlite-amalgamation-%version%.zip" (
        echo download sqlite-amalgamation-%version%.zip error!
        goto EOF
    )
)

7z x -y -o.\sqlite-amalgamation-%version% "sqlite-amalgamation-%version%.zip"

if exist ".\sqlite-amalgamation-%version%\sqlite-amalgamation-%version%" move /Y .\sqlite-amalgamation-%version%\sqlite-amalgamation-%version%\* .\sqlite-amalgamation-%version%\

if not exist ".\sqlite-amalgamation-%version%\shell.c" (
    echo copy shell.c from another location ...
    
    if not exist ".\sqlite-source-%version%.zip" (
        echo fetching sqlite-source-%version%.zip ...
        wget http://www.sqlite.org/sqlite-source-%version%.zip
        7z x -y -o.\sqlite-source-%version% "sqlite-source-%version%.zip"
        copy /y .\sqlite-source-%version%\shell.c .\sqlite-amalgamation-%version%\
        rd /s /q sqlite-source-%version%          
    )
    
    if not exist ".\sqlite-src-%version%.zip" (
        echo fetching sqlite-src-%version%.zip ...
        wget http://www.sqlite.org/sqlite-src-%version%.zip
        wget http://www.sqlite.org/2013/sqlite-src-%version%.zip
        7z x -y -o.\ "sqlite-src-%version%.zip"
        copy /y .\sqlite-src-%version%\shell.c .\sqlite-amalgamation-%version%\
        rd /s /q sqlite-src-%version% 
    )
  
)

if not exist ".\sqlite-amalgamation-%version%\sqlite3.def" (
    if not exist "sqlitedll-%version%.zip" (
        echo fetching sqlitedll-%version%.zip ...
        wget http://www.sqlite.org/sqlitedll-%version%.zip
        wget http://www.sqlite.org/2013/sqlitedll-%version%.zip
        7z x -y -o.\sqlitedll-%version% "sqlitedll-%version%.zip"
        copy /y .\sqlitedll-%version%\sqlite3.def .\sqlite-amalgamation-%version%\
        rd /s /q sqlitedll-%version%         
    )
    if not exist "sqlite-dll-win32-x86-%version%.zip" (
        echo fetching sqlite-dll-win32-x86-%version%.zip ...
        wget http://www.sqlite.org/sqlite-dll-win32-x86-%version%.zip
        wget http://www.sqlite.org/2013/sqlite-dll-win32-x86-%version%.zip
        7z x -y -o.\sqlite-dll-win32-x86-%version% "sqlite-dll-win32-x86-%version%.zip"
        copy /y .\sqlite-dll-win32-x86-%version%\sqlite3.def .\sqlite-amalgamation-%version%\
        rd /s /q sqlite-dll-win32-x86-%version%         
    )
)

cd /d sqlite-amalgamation-%version%
echo begin building sqlite-amalgamation-%version%

cl -Gs -GX -D_WIN32 -nologo -Zi -DOS_WIN=1 -DSQLITE_DEBUG=1 -DWIN32=1 -DTHREADSAFE=1 -DSQLITE_OS_WIN=1 -DSQLITE_ENABLE_COLUMN_METADATA=1 -DSQLITE_SOUNDEX=1 -DSQLITE_OMIT_LOAD_EXTENSION=1 -I. shell.c sqlite3.c -o sqlite3.exe

echo sqlite3.exe build finished ......

cl /nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "SQLITE3_EXPORTS" /D THREADSAFE=1 /D SQLITE_OS_WIN=1 /D SQLITE_ENABLE_COLUMN_METADATA=1 /D SQLITE_SOUNDEX=1 /D QLITE_OMIT_LOAD_EXTENSION=1 /D SQLITE_ENABLE_RTREE=1 /YX /Fp"sqlite3.pch" /FD /GZ /c /debug sqlite3.c

rem lib -out:"sqlite3.lib" sqlite3.obj

link.exe kernel32.lib user32.lib /nologo /dll /incremental:yes /debug /machine:I386 /pdb:"sqlite3.pdb" /def:"sqlite3.def" /out:"sqlite3.dll" /implib:"sqlite3.lib" /pdbtype:sept sqlite3.obj

echo sqlite3.dll build finished ......

echo end building sqlite-amalgamation-%version%
echo -----------------------------------------------
cd /d ..


:EOF
@echo on

